﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.calitha.goldparser.sql;
using System.Reflection;

namespace SqlFasterPaste
{
    public partial class Preview : Form
    {

        private string TemplatePath { get; set; }
        private CodeTemplateListWrapper TemplateList;
        private SQLParamExtract paramExtrac;
        private string oldSql = "";
        public string NewCode
        {
            get
            {
                return txtPreview.Text;
            }
            private set
            {
                txtPreview.Text = value;
            }
        }
        public event EventHandler OnAcceptCode;

        public Preview()
        {
            InitializeComponent();
            try
            {
               
                TemplatePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\CodeTemplate.xml";
                //paramExtrac = new SQLParamExtract(GrammarPath);
                TemplateList = new CodeTemplateListWrapper(TemplatePath);
                lstTemplates.DataSource = TemplateList;
                lstTemplates.DisplayMember = "TemplateName";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Preview_Load(object sender, EventArgs e)
        {

        }
        public void ShowClipBoard()
        {
            if (Clipboard.ContainsText())
            {
                ShowSql(Clipboard.GetText());
            }
        }
        public void ShowSql(string sql)
        {
            oldSql = sql;
            NewCode = "";
            TransformSql();
        }
        private void TransformSql()
        {
            //paramExtrac = new SQLParamExtract(GrammarPath);
            paramExtrac = new SQLParamExtract(Assembly.GetExecutingAssembly().GetManifestResourceStream("SqlFasterPaste.SQL-ANSI-89.cgt"));
            paramExtrac.Parse(oldSql);
            if (paramExtrac.TransformComppleteCorrectly)
            {
                CodeGenerator generator = new CodeGenerator();
                string code = generator.GenerateCode(paramExtrac.NewSql, paramExtrac.OldSql, paramExtrac.Replacements, TemplateList.SearchByName(lstTemplates.Text));
                NewCode = code;
                txtMessages.Text = "Preview Complete";
            }
            else
            {
                txtMessages.Text = "Not a correct sql statement, nothing to do here.";
                txtPreview.Text = oldSql;
            }
        }

        private void lstTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            TransformSql();
        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            if (OnAcceptCode != null)
            {
                OnAcceptCode.Invoke(sender, e);
            }
            this.Visible = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
    }
}
